home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / doc.zoo / doc / usrman / croff / hash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  5.9 KB  |  225 lines

  1. /*                        Copyright (c) 1988 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: hash.c,v 1.1 88/07/07 10:12:06 sau Exp $
  9.     $Source: /tmp/mgrsrc/doc/usrman/croff/RCS/hash.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/doc/usrman/croff/RCS/hash.c,v $$Revision: 1.1 $";
  12.  
  13. /************************************************************************
  14.  *
  15.  *    Simple Forms Package        Version 1.0    10/83
  16.  */
  17.  
  18. /* table lookup routines */
  19.  
  20. #include "hash.h"
  21. #include <stdio.h>
  22.  
  23. /*******************************************************************************
  24.  * Generate a hash table index from an arbitrary length string
  25.  * This is probably not a good algorithm
  26.  */
  27.  
  28. int
  29. Hash(key,max)
  30. char *key;        /* key to hash on */
  31. int max;        /* max should be a prime number */
  32.    {
  33.    register int i = 0;
  34.    register long sum;
  35.    for(sum = 0; *key != '\0';i++, sum += (*key++)<<(i&7));
  36.    return((int) (sum % max));
  37.    }
  38.  
  39. /*******************************************************************************
  40.  * Generate a hash table index from an arbitrary length string
  41.  * This is probably not a good algorithm
  42.  */
  43.  
  44. int
  45. hash(key,max)
  46. register char *key;        /* key to hash on */
  47. int max;            /* max should be a prime number */
  48.    {
  49.    register int sum;
  50.         
  51.    for (sum = *key; *key; sum += (*key) * (*(++key)));
  52.    return( (sum += *(key-2)) % max);
  53.    }
  54.  
  55. /*******************************************************************************
  56.  *    add an entry to the hash table, return count
  57.  */
  58.  
  59. int
  60. add_entry(table,size,name)
  61. struct table_entry *table[];    /* name of hash table */
  62. int size;            /* number of table entries */
  63. char *name;            /* name to be put in table */
  64.     {
  65.     int HASH(), index;
  66.     char *alloc(), *save_line();
  67.     register TABLE *list;
  68.  
  69.     index=HASH(name,size);
  70.     for(list=table[index]; list != (TABLE *) 0; list = list -> next)
  71.        if (Same(list->name,name)) {
  72.           list -> count += 1;
  73.           return(list -> count);
  74.           }
  75.     list = (TABLE *) alloc(sizeof (TABLE));
  76.     list -> name = save_line(name);
  77.     list -> next = table[index];
  78.     list -> value = (char *) 0;
  79.     list -> count = 1;
  80.     table[index] = list;
  81.     return(1);
  82.     }        
  83.  
  84. /*******************************************************************************
  85.  *    remove an entry to the hash table, return count
  86.  */
  87.  
  88. int
  89. dlt_entry(table,size,name)
  90. struct table_entry *table[];    /* pntr to hash table */
  91. int size;            /* size of hash table */
  92. char *name;            /* name to be put in table */
  93.     {
  94.     int HASH(), index;
  95.     void free();
  96.     register struct table_entry *list, *temp= (struct table_entry *) 0;
  97.     index=HASH(name,size);
  98.     for(list=table[index]; list != (TABLE *) 0; temp=list,list = list->next)
  99.        if (Same(list->name,name)) {
  100.           if (list -> count > 0)
  101.              list -> count -= 1;
  102.           if (list -> count == 0 && !(list->flags&HASH_STATIC)) {
  103.              free(list -> name);
  104.              if (list -> value)
  105.                  free(list -> value);
  106.              if (list == table[index]) {
  107.                 table[index] = list->next;
  108.                 free(list);
  109.                 }
  110.              else {
  111.                 temp->next = list->next;
  112.                     free(list);
  113.                 }
  114.          return(0);
  115.              }    
  116.           else return(list -> count);
  117.           }
  118.     return(-1);
  119.     }        
  120.  
  121. /*******************************************************************************
  122.  *    get an entry to the hash table, return value
  123.  */
  124.  
  125. char *
  126. get_entry(table,size,name)
  127. struct table_entry *table[];
  128. int size;
  129. char *name;            /* name to be put in table */
  130.     {
  131.     int HASH(), index;
  132.     register struct table_entry *list;
  133.  
  134.     index=HASH(name,size);
  135.     for(list=table[index]; list != (TABLE *) 0; list = list -> next)
  136.        if (Same(list->name,name)) {
  137.           return(list->count > 0 ? list -> value : (char *) 0);
  138.           }
  139.     return((char *) 0);
  140.     }        
  141.  
  142. /*******************************************************************************
  143.  *    see if item is in table 
  144.  */
  145.  
  146. int
  147. is_entry(table,size,name)
  148. struct table_entry *table[];
  149. int size;
  150. char *name;            /* name to be put in table */
  151.     {
  152.     int HASH(), index;
  153.     register struct table_entry *list;
  154.  
  155.     index=HASH(name,size);
  156.     for(list=table[index]; list != (TABLE *) 0; list = list -> next)
  157.        if (Same(list->name,name)) {
  158.           return(list->count);
  159.           }
  160.     return(0);
  161.     }        
  162.  
  163. /*******************************************************************************
  164.  *    put an entry to the hash table, return 1 if ok
  165.  */
  166.  
  167. int
  168. put_entry(table,size,name,value)
  169. struct table_entry *table[];    /* name of hash table */
  170. int size;            /* number of table entries */
  171. char *name;            /* name to be put in table */
  172. char *value;            /* value to be put into table */
  173.     {
  174.     int HASH(), index;
  175.     register struct table_entry *list;
  176.         char *save_line();
  177.     void free();
  178.  
  179.     index=HASH(name,size);
  180.     for(list=table[index]; list != (TABLE *) 0; list = list -> next)
  181.        if (Same(list->name,name) && !(list->flags&HASH_STATIC)) {
  182.           if (list -> value != (char *) 0) free(list -> value);
  183.           if (value != (char *) 0) list -> value = save_line(value);
  184.               else list->value == (char *) 0;
  185.           return(1);
  186.           }
  187.     return(0);
  188.     }        
  189.  
  190. /*******************************************************************************
  191.  *
  192.  *    allocate space for, and save a string; return its address
  193.  */
  194.  
  195. char *
  196. save_line(string)
  197. char *string;
  198.    {
  199.    char *where,*alloc(),*strcpy();
  200.  
  201.    if (string == (char *) 0) string = "";
  202.    where=alloc(strlen(string)+1);
  203.    strcpy(where,string);
  204.    return(where);
  205.    }
  206.  
  207. /*******************************************************************************
  208.  *
  209.  *    allocate some space
  210.  */
  211.  
  212. char *
  213. alloc(bytes)
  214. int bytes;
  215.   {
  216.   char *malloc(),*where;
  217.   void exit();
  218.  
  219.   if ((where=malloc((unsigned)bytes)) == NULL) {
  220.      fprintf(stderr,"no room for %d bytes\n",bytes);
  221.      exit(1);
  222.      }
  223.   return(where);
  224.   }    
  225.